﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx.CommonBot.Routines;
using Styx;
using Styx.TreeSharp;
using Styx.Common;
using System.Windows.Media;
using Styx.WoWInternals.WoWObjects;
using Attack = HolySword.Helpers.AttackManager;
using HolySword.Helpers;
using Logger = HolySword.Helpers.LogManager;
using Movement = HolySword.Helpers.MovementManager;
using TargetManager = HolySword.Helpers.TargetManager;
using HolySword.Delegates;

namespace HolySword
{
    public class HolySword : CombatRoutine
    {
        public static LocalPlayer Me = StyxWoW.Me;
        public override string Name { get { return "HolySword"; } }
        public override WoWClass Class { get { return WoWClass.Paladin; } }
        public override bool WantButton { get { return true; } }
        public override void OnButtonPress() { Logging.Write(Colors.Red, "config button pressed. nothing happens.\nHOORAY! we initilialize again..."); Initialize(); }
        private static WoWSpec mySpec;
        private static string _lastPulseMsg;

        /// <summary>
        /// Called when this CC is selected as the current CC.
        /// </summary>
        public override void Initialize()
        {
            if(mySpec == null || mySpec != Me.Specialization)
                mySpec = Me.Specialization;

            Logging.Write(Colors.White, "all credits to #weischbier (#The Saint)");
            Logging.Write(Colors.White, "_______________________________________\n");
            Logging.Write(Colors.Crimson, "=== HolySword is active ===\n");
            Logging.Write(Colors.Orange, "--- " + mySpec + " ---");
            Logging.Write(Colors.Orange, "#SpecChange on the Go: manually switch spec, click ClassConfig-Button - we initialize again!");
        }

        /// <summary>
        /// Behavior used when resting.
        /// </summary>
        public override Composite RestBehavior
        {
            get { return Behaviours.Get.Do("Resting"); }
        }

        /// <summary>
        /// Behavior used when engaging mobs in combat.
        /// </summary>
        public override Composite PullBehavior
        {
            get
            {
                return Behaviours.Get.Do("Pull", ret => !Me.CurrentTarget.ActiveAuras.ContainsKey("Deterrence") && !Me.CurrentTarget.ActiveAuras.ContainsKey("Divine Shield") && !Me.CurrentTarget.ActiveAuras.ContainsKey("Deterrence") && !Me.CurrentTarget.ActiveAuras.ContainsKey("Ice Block"), ret => Me.CurrentTarget);
            }
        }

        /// <summary>
        /// Behavior used for buffing, regular buffs like 'Power Word: Fortitude', 'MotW' etc..
        /// </summary>
        public override Composite PreCombatBuffBehavior
        {
            get { return Behaviours.Get.Do("PreCombat", ctx => !StyxWoW.Me.Mounted && !StyxWoW.Me.IsFlying && !StyxWoW.Me.IsOnTransport && !StyxWoW.Me.HasAura("Food") && !StyxWoW.Me.HasAura("Drink")); }
        }

        /// <summary>
        /// Behavior used in combat.
        /// </summary>
        public override Composite CombatBehavior
        {
            get
            {
                return Behaviours.Get.Do("Combat");
            }
        }

        public override void Pulse()
        {
            using (StyxWoW.Memory.AcquireFrame())
            {
                string _newPulseMsg = "";

                if (Me.IsMoving)
                {
                    if (Me.Combat)
                        _newPulseMsg = "combat";
                    else
                        _newPulseMsg = "noCombat";

                    if (_lastPulseMsg != _newPulseMsg && _lastPulseMsg == "noCombat")
                    {
                        Behaviours.Get.Do("Pull");
                        _lastPulseMsg = _newPulseMsg;
                        Logger.MovementLog(string.Format("#" + _newPulseMsg + " - MOVE => PULL"));
                    }
                    else if (_lastPulseMsg != _newPulseMsg)
                    {
                        _lastPulseMsg = _newPulseMsg;
                    }
                }
            }
        }

        /// <summary>
        /// PullDistance
        /// </summary>
        public override double? PullDistance
        {
            get
            {
                switch (mySpec)
                {
                    case WoWSpec.PaladinRetribution:
                        return 5f;
                    case WoWSpec.PaladinProtection:
                        return 5f;
                    case WoWSpec.PaladinHoly:
                        return 30f;
                    default:
                        return 5f;
                }
            }
        }
    }
}
